package tq.com.tqteacher.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.print.PrinterId;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.newPublish.TQJiangPingResultActivity;
import tq.com.tqteacher.discuss.activity.TQDiscussMsgActivity;
import tq.com.tqteacher.fragment.TQNewPublishFragment;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQNewPingJiangPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQTBNewPublish;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQBasePublishInfoView;
import tq.com.tqteacher.view.TQFanweiView;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.tqteacher.view.TQPriceSelectView;

public class TQNewPublishFXJActivity extends BaseTitleActivity {

    private TQNewFxjFragment mNewFxjFragment = new TQNewFxjFragment();
    static private int requestCode = 0x11;

    @Override
    protected BaseFragment fragment() {
        return mNewFxjFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);

        pHeader5View.getTitleView().setText(TQPublishType.title());
    }

    static public class TQNewFxjFragment extends BaseFragment{

        private TQBasePublishInfoView mBasePublishInfoView;
        private TQFanweiView mFanweiView;


        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View lView = inflater.inflate(R.layout.activity_newpublish_0_layout, container, false);

            lView.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoNext();
                }
            });
            mBasePublishInfoView = lView.findViewById(R.id.baseInfo);
            mFanweiView = lView.findViewById(R.id.area_panel);

            int grade = TQLogin.getCurrent().getUserInfo().getGrade();
            TQBasePublishInfoView.GradData lGradData = new TQBasePublishInfoView.GradData(grade, TQSystemDefine.gradeName(grade));
            ArrayList<TQBasePublishInfoView.GradData> lGradDataArrayList = new ArrayList<>();
            lGradDataArrayList.add(lGradData);
            mBasePublishInfoView.setGradData(lGradDataArrayList);

            if(TQPublishType.getSharePublishItem() != null && (TQPublishType.getSharePublishItem() instanceof TQTBNewPublish
            || TQPublishType.getSharePublishItem() instanceof TQNewPingJiangPublish)){
                mBasePublishInfoView.setPublishContent(TQPublishType.getSharePublishItem());
                mFanweiView.setSelect(TQPublishType.getSharePublishItem().getShow());
            }
            return lView;
        }



        private void gotoNext(){
            if(!mBasePublishInfoView.checkValid()){
                return;
            }

            TQPublishType.setSharePublishItemBaseInfo(mBasePublishInfoView.title(),
                    mBasePublishInfoView.desc(), mBasePublishInfoView.getCurrentGrade().getId());
            TQPublishType.getSharePublishItem().setShow(mFanweiView.getSelect());

            if(TQPublishType.getSharePublishItem().isModify()){

                TQPublishType.getSharePublishItem().onModifyBaseInfo(new INetCallback() {
                    @Override
                    public void result(TQNetResult pNetResult) {
                        if(!pNetResult.isSuccess()){
                            AlertDialogHelper.show(TQNewFxjFragment.this.getContext(), pNetResult.getErrMsg());
                            return;
                        }

                        TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                        TQNewFxjFragment.this.getActivity().finish();
                    }
                });
            }else{
                TQPublishType.getSharePublishItem().onPulishComplete(new INetCallback() {
                    @Override
                    public void result(TQNetResult pNetResult) {
                        Log.d(TAG, "result: ");
                        if(!pNetResult.isSuccess()){
                            AlertDialogHelper.show(TQNewFxjFragment.this.getContext(), pNetResult.getErrMsg());
                            return;
                        }

                        TQPublishType.getSharePublishItem().onNewPublishResult(pNetResult.getData());
                        if(TQPublishType.getSharePublishItem().getType() == TQPublishType.TYPE_JP){
                            TQJiangPingResultActivity.show(TQNewFxjFragment.this.getActivity());
                        }else{
                            //
                            TQUploadFinishActivity.show(TQNewFxjFragment.this.getActivity());
                        }

                        TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.add, null));
                        TQNewFxjFragment.this.getActivity().finish();
                    }
                });
            }
        }



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode != RESULT_OK){
            Log.d(TAG, "onActivityResult: ");
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
